package com.zhuangfei.hos_timetableview.adapter;

import com.zhuangfei.hos_timetableview.ResourceTable;
import com.zhuangfei.timetable.model.Schedule;
import com.zhuangfei.timetable.model.ScheduleColorPool;
import com.zhuangfei.timetable.utils.ElementUtil;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.RecycleItemProvider;
import ohos.agp.components.Text;
import ohos.app.Context;

import java.util.List;

/**
 * Created by Liu ZhuangFei on 2018/6/18.
 */
public class NonViewAdapter extends RecycleItemProvider {

    List<Schedule> schedules;
    Context context;
    LayoutScatter inflater;

    public NonViewAdapter(Context context, List<Schedule> schedules) {
        this.context = context;
        this.schedules = schedules;
        inflater = LayoutScatter.getInstance(context);
    }

    @Override
    public int getCount() {
        return schedules.size();
    }

    @Override
    public Object getItem(int position) {
        return schedules.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Component getComponent(int position, Component convertComponent, ComponentContainer componentContainer) {
        ViewHolder holder;
        if (convertComponent == null) {
            holder = new ViewHolder();
            convertComponent = inflater.parse(ResourceTable.Layout_item_nonview, null, false);
            holder.nameTextView = (Text) convertComponent.findComponentById(ResourceTable.Id_id_nonview_name);
            holder.colorTextView = (Text) convertComponent.findComponentById(ResourceTable.Id_id_nonview_color);
            convertComponent.setTag(holder);
        } else {
            holder = (ViewHolder) convertComponent.getTag();
        }
        Schedule schedule = (Schedule) getItem(position);
        ScheduleColorPool colorPool = new ScheduleColorPool(context);
        holder.nameTextView.setText(schedule.getName());
        holder.colorTextView.setBackground(ElementUtil.getShapeElement6(colorPool.getColorAuto(schedule.getColorRandom())));
        return convertComponent;
    }

    class ViewHolder {
        Text nameTextView;
        Text colorTextView;
    }

}
